﻿using GetSmartIDEList.Core.Ulities;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace GetSmartIDEList.Lib.Common
{
    public static class HttpPost
    {
        /// <summary>  
        ///  Http同步Post同步请求  
        /// </summary>  
        /// <param name="url">Url地址</param>  
        /// <param name="postStr">请求Url数据</param>  
        /// <param name="encode">编码(默认UTF8)</param>  
        /// <returns></returns>  
        public static bool Post(string url, string postStr, out string result, string ContentType = "application/json",
            Encoding encode = null, string token = null, bool othersHead = false, Action<WebClient> T1 = null)
        {
            result = string.Empty;

            try
            {
                WebClient webClient = new() { Encoding = Encoding.UTF8 };

                if (encode != null)
                {
                    webClient.Encoding = encode;
                }

                if (string.IsNullOrWhiteSpace(postStr))
                {
                    postStr = "";
                }

                byte[] sendData = Encoding.Default.GetBytes(postStr);

                webClient.Headers.Add("Content-Type", ContentType);
                webClient.Headers.Add("ContentLength", sendData.Length.ToString(CultureInfo.InvariantCulture));

                if (othersHead && T1 != null)
                {
                    T1(webClient);
                }

                if (token != null)
                {
                    webClient.Headers.Add("Authorization", token);
                }

                byte[] readData = webClient.UploadData(url, "POST", sendData);

                result = Encoding.UTF8.GetString(readData);
                return true;
            }
            catch (Exception ex)
            {
                result = ex.Message;
                return false;
            }
        }

        /// <summary>
        /// 请求
        /// </summary>
        /// <param name="url"></param>
        /// <param name="parameters"></param>
        /// <param name="encoding"></param>
        /// <param name="contentType"></param>
        /// <param name="timeout"></param>
        /// <returns></returns>
        public static string HttpWebRequest(string url, string parameters, Encoding encoding, string contentType, int timeout = 120000)
        {
            string result = "";

            try
            {
                var request = (HttpWebRequest)WebRequest.Create(url);
                request.Timeout = timeout;
                request.CookieContainer = null;
                byte[] postData = encoding.GetBytes(parameters);
                request.Method = "POST";
                request.ContentType = contentType;
                request.ContentLength = postData.Length;
                using (Stream stream = request.GetRequestStream())
                {
                    stream.Write(postData, 0, postData.Length);
                }
                var response = (HttpWebResponse)request.GetResponse();

                using (Stream stream = response.GetResponseStream())
                {
                    if (stream == null)
                        return string.Empty;
                    using (var reader = new StreamReader(stream, encoding))
                    {
                        result = reader.ReadToEnd();
                    }
                }
                return result;
            }
            catch (Exception ex)
            {
                return result;
            }
            finally
            {

            }
        }
    }
}
